package net.shopxx.entity;

import java.math.BigDecimal;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Transient;

import net.shopxx.util.SystemConfigUtil;

/**
 * 实体类 - 购物车项
 */

@Entity
@DiscriminatorValue(value="p")
public class CartItemProduct extends CartItem {

	private static final long serialVersionUID = -4241469437632553865L;
	private Productitem productitem;// 商品

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(nullable = false)
	public Productitem getProductitem() {
		return productitem;
	}

	public void setProductitem(Productitem productitem) {
		this.productitem = productitem;
	}

	// 获取优惠价格，若member对象为null则返回原价格
	@Transient
	public BigDecimal getPreferentialPrice() {
		if (member != null) {
			BigDecimal preferentialPrice = productitem.getProduct().getPrice().multiply(
					new BigDecimal(member.getMemberRank()
							.getPreferentialScale().toString())
							.divide(new BigDecimal("100")));
			return SystemConfigUtil.getPriceScaleBigDecimal(preferentialPrice);
		} else {
			return productitem.getProduct().getPrice();
		}
	}

	// 获取小计价格
	@Transient
	public BigDecimal getSubtotalPrice() {
		BigDecimal subtotalPrice = getPreferentialPrice().multiply(
				new BigDecimal(quantity.toString()));
		return SystemConfigUtil.getOrderScaleBigDecimal(subtotalPrice);
	}

}